home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Mail
/
pine3.92
/
pine
/
osdep
/
tempnam.dos
< prev
next >
Wrap
Text File
|
1994-08-24
|
2KB
|
76 lines
#line 2 "osdep/tempnam.dos"
/*
* This routine is derived from BSD4.3 code,
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)tmpnam.c 4.5 (Berkeley) 6/27/88";
#endif /* LIBC_SCCS and not lint */
/*----------------------------------------------------------------------
Return a unique file name in a given directory. This is not quite
the same as the usual tempnam() function, though it is very similar.
We want it to use the TMP environment variable only if dir is NULL,
instead of using TMP regardless if it is set.
Args: dir -- The directory to create the name in
prefix -- Prefix of the name
Result: Malloc'd string equal to new name is returned. It must be free'd
by the caller. Returns the string on success and NULL on failure.
----*/
char *
temp_nam(dir, prefix)
char *dir, *prefix;
{
struct stat buf;
char *f, *name;
char *mktemp();
if (!(name = malloc((unsigned int)MAXPATH)))
return(NULL);
if (!dir && ((f = getenv("TMP")) || (f = getenv("TEMP"))) &&
!stat(f, &buf) && (buf.st_mode&S_IFMT) == S_IFDIR &&
!can_access(f, WRITE_ACCESS)) {
(void)strcpy(name, f);
goto done;
}
if (dir) {
strcpy(name, dir);
if(!*dir || (isalpha(*dir) && *(dir+1) == ':' && !*(dir+2)))
strcat(name, "\\");
if((!stat(name, &buf) && (buf.st_mode&S_IFMT) == S_IFDIR)
&& !can_access(name, WRITE_ACCESS)) {
(void)strcpy(name, dir);
goto done;
}
}
#ifndef P_tmpdir
#define P_tmpdir "\\tmp"
#endif
if (!stat(P_tmpdir, &buf) &&
(buf.st_mode&S_IFMT) == S_IFDIR &&
!can_access(P_tmpdir, WRITE_ACCESS)) {
(void)strcpy(name, P_tmpdir);
goto done;
}
free(name);
return(NULL);
done:
if(name[0] && name[strlen(name)-1] != '\\')
(void)strcat(name, "\\");
if (prefix)
(void)strcat(name, prefix);
(void)strcat(name, "XXXXXX");
return(mktemp(name));
}